<template>
  <el-container style="height: 100%">
    <!-- 添加 -->
    <el-tooltip
      effect="dark"
      content="业绩添加"
      placement="top"
      v-show="addBtnIsShow"
      class="addAchievementBtn"
    >
      <el-button
        type="primary"
        icon="el-icon-circle-plus"
        circle
        @click="addAchievement"
      ></el-button>
    </el-tooltip>

    <el-header style="height: 12%" class="manageTitle">
      <!-- Header content -->
      {{ manageTitle }}
    </el-header>
    <el-container
      class="manageContainer"
      v-show="achievementManageContainerIsShow"
    >
      <span>
        <p>{{ achievementData.achievementName }}</p>
      </span>
      <span>
        <!-- 查看 -->
        <el-tooltip
          class="item"
          effect="dark"
          content="业绩查看"
          placement="top"
        >
          <el-button
            type="primary"
            icon="el-icon-s-platform"
            circle
            @click="showAchievementDetail"
          ></el-button>
        </el-tooltip>
      </span>
      <span>
        <!-- 编辑 -->
        <el-tooltip
          class="item"
          effect="dark"
          content="业绩编辑"
          placement="top"
        >
          <el-button
            type="primary"
            icon="el-icon-s-tools"
            circle
            @click="editAchievement"
          ></el-button>
        </el-tooltip>
      </span>
      <span
        ><!-- 删除 -->
        <el-tooltip
          class="item"
          effect="dark"
          content="业绩删除"
          placement="top"
        >
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="deleteAchievement"
          ></el-button>
        </el-tooltip>
      </span>
      <!-- 状态 -->
      <span>
        审批状态：<el-switch
          v-model="achievementData.status"
          v-show="achievementManageContainerIsShow"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="通过"
          inactive-text="拒绝"
          @change="changeAchievementStatus"
        >
        </el-switch
      ></span>
    </el-container>
    <myAwardDetail />
    <myEditAward :editAwardData="achievementData" />
    <my-award-add></my-award-add>

    <myEssayDetail />
    <myEditEssay />
    <my-essay-add></my-essay-add>

    <myPatentDetail />
    <myEditPatent />
    <my-patent-add></my-patent-add>

    <myProjectDetail />
    <myEditProject />
    <my-project-add></my-project-add>

    <myWorkDetail />
    <myEditWork />
    <my-work-add></my-work-add>
  </el-container>
</template>

<script>
// 获奖
import myAwardDetail from "../award/myAwardDetail.vue";
import myEditAward from "../award/myEditAward.vue";
import myAwardAdd from "../award/myAwardAdd.vue";
// 论文
import myEssayDetail from "../essay/myEssayDetail.vue";
import myEssayAdd from "../essay/myEssayAdd.vue";
import myEditEssay from "../essay/myEditEssay";
// 专利
import myPatentDetail from "../patent/myPatentDetail.vue";
import myPatentAdd from "../patent/myPatentAdd";
import myEditPatent from "../patent/myEditPatent";
// 项目
import myProjectDetail from "../project/myProjectDetail.vue";
import myProjectAdd from "../project/myProjectAdd.vue";
import myEditProject from "../project/myEditProject";
// 著作与教材
import myWorkDetail from "../work/myWorkDetail.vue";
import myWorkAdd from "../work/myWorkAdd.vue";
import myEditWork from "../work/myEditWork";

export default {
  name: "myAchievementManage",
  props: ["achievementManageContainerIsShow"],
  data() {
    return {
      achievementData: [],
      cacheData: [],
      manageTitle: "所有业绩管理",
      addBtnIsShow: true,
    };
  },
  components: {
    myAwardDetail,
    myEditAward,
    myAwardAdd,
    myEssayDetail,
    myEditEssay,
    myEssayAdd,
    myPatentDetail,
    myEditPatent,
    myPatentAdd,
    myProjectDetail,
    myEditProject,
    myProjectAdd,
    myWorkDetail,
    myEditWork,
    myWorkAdd,
  },
  methods: {
    showAchievementDetail() {
      // console.log(this.achievementData);
      let { achievementType } = this.achievementData;
      if (achievementType == "获奖") {
        this.$bus.$emit("handleAwardDetailShow", this.achievementData);
      } else if (achievementType == "论文") {
        this.$bus.$emit("handleEssayDetailShow", this.achievementData);
      } else if (achievementType == "专利") {
        this.$bus.$emit("handlePatentDetailShow", this.achievementData);
      } else if (achievementType == "项目") {
        this.$bus.$emit("handleProjectDetailShow", this.achievementData);
      } else if (achievementType == "著作与教材") {
        this.$bus.$emit("handleWorkDetailShow", this.achievementData);
      }
    },
    editAchievement() {
      let { achievementType } = this.achievementData;
      if (achievementType == "获奖") {
        this.$bus.$emit(
          "handleIsAwardEditDialogVisibleShow",
          this.achievementData
        );
      } else if (achievementType == "论文") {
        this.$bus.$emit(
          "handleIsEssayEditDialogVisibleShow",
          this.achievementData
        );
      } else if (achievementType == "专利") {
        this.$bus.$emit(
          "handleIsWorkEditDialogVisibleShow",
          this.achievementData
        );
      } else if (achievementType == "项目") {
        this.$bus.$emit(
          "handleIsProjectEditDialogVisibleShow",
          this.achievementData
        );
      } else if (achievementType == "著作与教材") {
        this.$bus.$emit(
          "handleIsWorkEditDialogVisibleShow",
          this.achievementData
        );
      }
    },
    async changeAchievementStatus(val) {
      let { data: res } = await this.$axios.post("/admin/achievementStatus", {
        achievementMsg: {
          status: val,
          achievementType: this.achievementData.achievementType,
          id: this.achievementData.id,
        },
      });
      console.log(res);
    },
    async deleteAchievement() {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let { id, achievementType } = this.achievementData;
          if (achievementType == "获奖") {
            let { awardfile } = this.achievementData;
            let { data: res } = await this.$axios.delete("/award", {
              params: {
                id,
                awardfile,
              },
            });
            if (res.status !== 200) {
              return this.$message.error("删除失败");
            }
            this.$message.success("获奖业绩删除成功");
            this.$bus.$emit("getAchievementList");
          } else if (achievementType == "论文") {
            let { essayfile } = this.achievementData;
            let { data: res } = await this.$axios.delete("/essay", {
              params: {
                id,
                essayfile,
              },
            });
            if (res.status !== 200) {
              return this.$message.error("删除失败");
            }
            this.$message.success("论文业绩删除成功");
            this.$bus.$emit("getAchievementList");
          } else if (achievementType == "专利") {
            let { patentfile } = this.achievementData;
            let { data: res } = await this.$axios.delete("/patent", {
              params: {
                id,
                patentfile,
              },
            });
            if (res.status !== 200) {
              return this.$message.error("删除失败");
            }
            this.$message.success("专利业绩删除成功");
            this.$bus.$emit("getAchievementList");
          } else if (achievementType == "项目") {
            let { projectfile } = this.achievementData;
            let { data: res } = await this.$axios.delete("/project", {
              params: {
                id,
                projectfile,
              },
            });
            if (res.status !== 200) {
              return this.$message.error("删除失败");
            }
            this.$message.success("专利业绩删除成功");
            this.$bus.$emit("getAchievementList");
          } else if (achievementType == "著作与教材") {
            let { workfile } = this.achievementData;
            let { data: res } = await this.$axios.delete("/work", {
              params: {
                id,
                workfile,
              },
            });
            if (res.status !== 200) {
              return this.$message.error("删除失败");
            }
            this.$message.success("著作与教材业绩删除成功");
            this.$bus.$emit("getAchievementList");
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addAchievement() {
      if (this.manageTitle == "获奖业绩管理") {
        this.$bus.$emit("handleAwardAddShow");
      } else if (this.manageTitle == "论文业绩管理") {
        this.$bus.$emit("handleEssayAddShow");
      } else if (this.manageTitle == "专利业绩管理") {
        this.$bus.$emit("handlePatentAddShow");
      } else if (this.manageTitle == "项目业绩管理") {
        this.$bus.$emit("handleProjectAddShow");
      } else if (this.manageTitle == "著作与教材业绩管理") {
        this.$bus.$emit("handleWorkAddShow");
      }
    },
    handleAchievementType({ data }) {
      this.cacheData = [];
      this.cacheData = JSON.parse(JSON.stringify([data]));

      let achievementType = this.cacheData[0].achievementType;
      // console.log(this.cacheData);

      if (data) {
        if (achievementType == "获奖") {
          this.cacheData.forEach((item) => {
            this.changeAuthorName(item, "author", "awardee");
            this.changeAuthorName(item, "achievementName", "name");
            this.changeAuthorName(item, "college", "release_unit");
          });
        } else if (achievementType == "论文") {
          this.cacheData.forEach((item) => {
            this.changeAuthorName(item, "achievementName", "name");
            this.changeAuthorName(item, "college", "author_unit");
          });
        } else if (achievementType == "专利") {
          this.cacheData.forEach((item) => {
            this.changeAuthorName(item, "author", "inventor");
            this.changeAuthorName(item, "achievementName", "name");
            this.changeAuthorName(item, "college", "unit");
          });
        } else if (achievementType == "项目") {
          this.cacheData.forEach((item) => {
            this.changeAuthorName(item, "author", "commander");
            this.changeAuthorName(item, "achievementName", "name");
            this.changeAuthorName(item, "college", "release_unit");
          });
        } else if (achievementType == "著作与教材") {
          this.cacheData.forEach((item) => {
            this.changeAuthorName(item, "achievementName", "name");
            this.changeAuthorName(item, "college", "affiliated_unit");
          });
        }
        this.achievementData = this.cacheData[0];
        // console.log(this.achievementData);
      }
    },
    changeAuthorName(item, oldName, newName) {
      for (const key in item) {
        if (key == oldName) {
          this.$set(item, newName, item[key]);
          delete item[key];
        }
        if (key == "status") {
          if (item[key] == "1") {
            this.$set(item, key, true);
          } else {
            this.$set(item, key, false);
          }
        }
      }
    },
    achievementInit(val) {
      let { title, addBtnIsShow } = val;
      this.manageTitle = title + "业绩管理";
      addBtnIsShow === "no"
        ? (this.addBtnIsShow = false)
        : (this.addBtnIsShow = true);
    },
  },
  mounted() {
    this.$bus.$on("handleAchievementType", this.handleAchievementType);
    this.$bus.$on("achievementInit", this.achievementInit);
  },
  beforeDestroy() {
    this.$bus.$off("handleAchievementType");
    this.$bus.$off("achievementInit");
  },
};
</script>

<style lang = "less" scoped>
.manageTitle {
  font-size: 26px;
  text-align: center;
}

.manageContainer {
  height: 92%;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  font-weight: 700;
  > span {
    transform: scale(2);
    height: 100px;
    transition: all 0.4s;
    transform-origin: center center;
    &:nth-of-type(2):hover {
      transform: scale(2.1);
    }
  }
}

p {
  font-size: 2px !important;
}

/deep/.el-tooltip {
  margin: 0px;
}

.addAchievementBtn {
  position: absolute;
}
</style>